home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-xapian / examples / simpleexpand.py next >
Encoding:
Python Source  |  2009-02-22  |  3.2 KB  |  99 lines

  1. #!/usr/bin/env python
  2. #
  3. # Simple example script demonstrating query expansion.
  4. #
  5. # Copyright (C) 2003 James Aylett
  6. # Copyright (C) 2004,2006,2007 Olly Betts
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License as
  10. # published by the Free Software Foundation; either version 2 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
  21. # USA
  22.  
  23. import sys
  24. import xapian
  25.  
  26. # We require at least two command line arguments.
  27. if len(sys.argv) < 3:
  28.     print >> sys.stderr, "Usage: %s PATH_TO_DATABASE QUERY [-- [DOCID...]]" % sys.argv[0]
  29.     sys.exit(1)
  30.  
  31. try:
  32.     # Open the database for searching.
  33.     database = xapian.Database(sys.argv[1])
  34.  
  35.     # Start an enquire session.
  36.     enquire = xapian.Enquire(database)
  37.  
  38.     # Combine command line arguments up to "--" with spaces between
  39.     # them, so that simple queries don't have to be quoted at the shell
  40.     # level.
  41.     query_string = sys.argv[2]
  42.     index = 3
  43.     while index < len(sys.argv):
  44.         arg = sys.argv[index]
  45.         index += 1
  46.         if arg == '--':
  47.             # Passed marker, move to parsing relevant docids.
  48.             break
  49.         query_string += ' '
  50.         query_string += arg
  51.  
  52.     # Create an RSet with the listed docids in.
  53.     reldocs = xapian.RSet()
  54.     for index in xrange(index, len(sys.argv)):
  55.         reldocs.add_document(int(sys.argv[index]))
  56.  
  57.     # Parse the query string to produce a Xapian::Query object.
  58.     qp = xapian.QueryParser()
  59.     stemmer = xapian.Stem("english")
  60.     qp.set_stemmer(stemmer)
  61.     qp.set_database(database)
  62.     qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
  63.     query = qp.parse_query(query_string)
  64.  
  65.     if not query.empty():
  66.         print "Parsed query is: %s" % query.get_description()
  67.  
  68.         # Find the top 10 results for the query.
  69.         enquire.set_query(query)
  70.         matches = enquire.get_mset(0, 10, reldocs)
  71.  
  72.         # Display the results.
  73.         print "%i results found." % matches.get_matches_estimated()
  74.         print "Results 1-%i:" % matches.size()
  75.  
  76.         for m in matches:
  77.             print "%i: %i%% docid=%i [%s]" % (m.rank + 1, m.percent, m.docid, m.document.get_data())
  78.  
  79.     # Put the top 5 (at most) docs into the rset if rset is empty
  80.     if reldocs.empty():
  81.         i = matches.begin()
  82.         for j in xrange(1, 5):
  83.             reldocs.add_document(i.get_docid())
  84.             i.next()
  85.             if i == matches.end():
  86.                 break
  87.  
  88.     # Get the suggested expand terms
  89.     eterms = enquire.get_eset(10, reldocs)
  90.     print "%i suggested additional terms" % eterms.size()
  91.     k = eterms.begin()
  92.     while k != eterms.end():
  93.         print "%s: %f" % (k.get_term(), k.get_weight())
  94.         k.next()
  95.  
  96. except Exception, e:
  97.     print >> sys.stderr, "Exception: %s" % str(e)
  98.     sys.exit(1)
  99.